library(plotly)
library(tidyverse)
library(data.table)
library(DT)
library(ggthemes)
library(patchwork)
usd_kes_hist <- fread("USD_KES Historical Data.csv")
usd_kes_hist[, Date :=  as.Date(Date, format = "%b %d,%Y")]
x <- as.Date("2002-12-01")
x_end <- as.Date("2003-01-01")

y <-90
y_end <- 77.500

x_uhuru <- as.Date("2013-03-09")
x_end_uhuru <- as.Date("2013-04-09")

y_uhuru <-105
y_end_uhuru <- 85.600

dollar <- ggplot(usd_kes_hist,aes(Date, Price))+
    geom_line()+
    scale_x_date(date_breaks = "3 year", date_labels = "%b-%y")+
    labs(y = "1 USD to KES", title = "Historical Prices USD to KES")+
  
    annotate(
    geom = "curve", x = x, y = y, xend = x_end, yend = y_end, 
    curvature = .3, arrow = arrow(length = unit(3, "mm"))
  ) +
  annotate(geom = "text", x = x, y = y,
           label = "Pres Kibaki \n takes Office", hjust = "left",
           angle = 30)+
    annotate(
    geom = "curve", x = x_uhuru, y = y_uhuru, 
    xend = x_end_uhuru, yend = y_end_uhuru, 
    curvature = .1, arrow = arrow(length = unit(3, "mm"))
  ) +
  annotate(geom = "text", x = x_uhuru, y = y_uhuru+2,
           label = "Pres Uhuru \n takes Office", hjust = "left",
           angle = 30)
dollar

kenya_debt <- read_csv( "kenya_debt/Public Debt (Ksh Million).csv") %>% setDT()

kenya_debt[, Date := as.Date(paste(Year, Month, "01", sep = "-"), format = "%Y-%B-%d")]
kenya_debt[, perc_external := round(`External Debt`/Total* 100, 1)]
kenya_debt[, Total := Total/1000000]
x <- as.Date("2002-12-01")
x_end <- as.Date("2003-01-01")

y <-2.000000
y_end <- .6152281

x_uhuru <- as.Date("2013-03-09")
x_end_uhuru <- as.Date("2013-04-09")

y_uhuru <-4.000000
y_end_uhuru <- 1.8824059


debt <- ggplot(kenya_debt, aes(Date, Total)) +
    geom_line()+
  
    scale_x_date(date_breaks = "3 year", date_labels = "%b-%y")+
  
    labs(title = "Kenya Debt from 1999 to 2020 June",
         y = "Total Debt in Trillions(KES)")+
  
    annotate(
    geom = "curve", x = x, y = y, xend = x_end, yend = y_end, 
    curvature = .3, arrow = arrow(length = unit(2, "mm"))
  ) +
  annotate(geom = "text", x = x, y = y,
           label = "Pres Kibaki \n takes Office", hjust = "left",
           angle = 30)+
  
    annotate(
    geom = "curve", x = x_uhuru, y = y_uhuru, 
    xend = x_end_uhuru, yend = y_end_uhuru, 
    curvature = -.1, arrow = arrow(length = unit(3, "mm"))
  ) +
  
  annotate(geom = "text", x = x_uhuru, y = y_uhuru+.5,
           label = "Pres Uhuru \n takes Office", hjust = "right",
           angle = 30)
 

debt

my_breaks <- seq(15, 70, 5)
external <- ggplot(kenya_debt, aes(Date, perc_external)) +
    geom_line()+
  
    scale_x_date(date_breaks = "2 year", date_labels = "%Y")+
  
    labs(title = "Kenya External Debt % from 1999 to 2020 June",
         y = "External Debt (%)")+
    theme_hc()+
    scale_y_continuous(breaks = my_breaks )

external

debt_data <- fread("poverty_data/IDS-DRSCountries_WLD_Data.csv")
nms_old <-debt_data[1,]  %>% as.character()
nms_old
##  [1] "Country Name"          "Country Code"          "Counterpart-Area Name"
##  [4] "Counterpart-Area Code" "Series Name"           "Series Code"          
##  [7] "1970"                  "1971"                  "1972"                 
## [10] "1973"                  "1974"                  "1975"                 
## [13] "1976"                  "1977"                  "1978"                 
## [16] "1979"                  "1980"                  "1981"                 
## [19] "1982"                  "1983"                  "1984"                 
## [22] "1985"                  "1986"                  "1987"                 
## [25] "1988"                  "1989"                  "1990"                 
## [28] "1991"                  "1992"                  "1993"                 
## [31] "1994"                  "1995"                  "1996"                 
## [34] "1997"                  "1998"                  "1999"                 
## [37] "2000"                  "2001"                  "2002"                 
## [40] "2003"                  "2004"                  "2005"                 
## [43] "2006"                  "2007"                  "2008"                 
## [46] "2009"                  "2010"                  "2011"                 
## [49] "2012"                  "2013"                  "2014"                 
## [52] "2015"                  "2016"                  "2017"                 
## [55] "2018"                  "2019"                  "2020"                 
## [58] "2021"                  "2022"                  "2023"                 
## [61] "2024"                  "2025"                  "2026"                 
## [64] "2027"
debt_data <-debt_data[-1,]
names(debt_data) <- nms_old
nms_new <- nms_old %>% tolower() 
nms_new <- gsub("\\s|-", "_", nms_new)
nms_new
##  [1] "country_name"          "country_code"          "counterpart_area_name"
##  [4] "counterpart_area_code" "series_name"           "series_code"          
##  [7] "1970"                  "1971"                  "1972"                 
## [10] "1973"                  "1974"                  "1975"                 
## [13] "1976"                  "1977"                  "1978"                 
## [16] "1979"                  "1980"                  "1981"                 
## [19] "1982"                  "1983"                  "1984"                 
## [22] "1985"                  "1986"                  "1987"                 
## [25] "1988"                  "1989"                  "1990"                 
## [28] "1991"                  "1992"                  "1993"                 
## [31] "1994"                  "1995"                  "1996"                 
## [34] "1997"                  "1998"                  "1999"                 
## [37] "2000"                  "2001"                  "2002"                 
## [40] "2003"                  "2004"                  "2005"                 
## [43] "2006"                  "2007"                  "2008"                 
## [46] "2009"                  "2010"                  "2011"                 
## [49] "2012"                  "2013"                  "2014"                 
## [52] "2015"                  "2016"                  "2017"                 
## [55] "2018"                  "2019"                  "2020"                 
## [58] "2021"                  "2022"                  "2023"                 
## [61] "2024"                  "2025"                  "2026"                 
## [64] "2027"
setnames(debt_data, nms_old, nms_new)
id_vars <- c("country_name", "country_code", "counterpart_area_name", 
             "counterpart_area_code", "series_name", "series_code")

debt_data <- melt(debt_data,
                                id.vars = id_vars, 
                           variable.factor = F,
                           value.factor = F,
                           variable.name = "year")

debt_data[, year := str_trim(year)]
debt_data[, year := as.numeric(year)]
debt_data <-  debt_data[!is.na(value)]
indicator_name <- c("Currency composition of PPG debt, U.S. dollars (%)",
                    "Interest payments on external debt (% of exports of goods, services and primary income)",
                    "Interest payments on external debt (% of GNI)",
                     "Short-term debt (% of total external debt)",
                     "Multilateral debt (% of total external debt)" )

debt_data[, unique(country_name)]
##   [1] "Afghanistan"                                       
##   [2] "Albania"                                           
##   [3] "Algeria"                                           
##   [4] "Angola"                                            
##   [5] "Argentina"                                         
##   [6] "Armenia"                                           
##   [7] "Azerbaijan"                                        
##   [8] "Bangladesh"                                        
##   [9] "Belarus"                                           
##  [10] "Belize"                                            
##  [11] "Benin"                                             
##  [12] "Bhutan"                                            
##  [13] "Bolivia"                                           
##  [14] "Bosnia and Herzegovina"                            
##  [15] "Botswana"                                          
##  [16] "Brazil"                                            
##  [17] "Bulgaria"                                          
##  [18] "Burkina Faso"                                      
##  [19] "Burundi"                                           
##  [20] "Cabo Verde"                                        
##  [21] "Cambodia"                                          
##  [22] "Cameroon"                                          
##  [23] "Central African Republic"                          
##  [24] "Chad"                                              
##  [25] "China"                                             
##  [26] "Colombia"                                          
##  [27] "Comoros"                                           
##  [28] "Congo, Dem. Rep."                                  
##  [29] "Congo, Rep."                                       
##  [30] "Costa Rica"                                        
##  [31] "Cote d'Ivoire"                                     
##  [32] "Djibouti"                                          
##  [33] "Dominica"                                          
##  [34] "Dominican Republic"                                
##  [35] "East Asia & Pacific (excluding high income)"       
##  [36] "Ecuador"                                           
##  [37] "Egypt, Arab Rep."                                  
##  [38] "El Salvador"                                       
##  [39] "Eritrea"                                           
##  [40] "Eswatini"                                          
##  [41] "Ethiopia"                                          
##  [42] "Europe & Central Asia (excluding high income)"     
##  [43] "Fiji"                                              
##  [44] "Gabon"                                             
##  [45] "Gambia, The"                                       
##  [46] "Georgia"                                           
##  [47] "Ghana"                                             
##  [48] "Grenada"                                           
##  [49] "Guatemala"                                         
##  [50] "Guinea"                                            
##  [51] "Guinea-Bissau"                                     
##  [52] "Guyana"                                            
##  [53] "Haiti"                                             
##  [54] "Honduras"                                          
##  [55] "IDA only"                                          
##  [56] "India"                                             
##  [57] "Indonesia"                                         
##  [58] "Iran, Islamic Rep."                                
##  [59] "Jamaica"                                           
##  [60] "Jordan"                                            
##  [61] "Kazakhstan"                                        
##  [62] "Kenya"                                             
##  [63] "Kosovo"                                            
##  [64] "Kyrgyz Republic"                                   
##  [65] "Lao PDR"                                           
##  [66] "Latin America & Caribbean (excluding high income)" 
##  [67] "Least developed countries: UN classification"      
##  [68] "Lebanon"                                           
##  [69] "Lesotho"                                           
##  [70] "Liberia"                                           
##  [71] "Low & middle income"                               
##  [72] "Low income"                                        
##  [73] "Lower middle income"                               
##  [74] "Madagascar"                                        
##  [75] "Malawi"                                            
##  [76] "Maldives"                                          
##  [77] "Mali"                                              
##  [78] "Mauritania"                                        
##  [79] "Mexico"                                            
##  [80] "Middle East & North Africa (excluding high income)"
##  [81] "Middle income"                                     
##  [82] "Moldova"                                           
##  [83] "Mongolia"                                          
##  [84] "Montenegro"                                        
##  [85] "Morocco"                                           
##  [86] "Mozambique"                                        
##  [87] "Myanmar"                                           
##  [88] "Nepal"                                             
##  [89] "Nicaragua"                                         
##  [90] "Niger"                                             
##  [91] "Nigeria"                                           
##  [92] "North Macedonia"                                   
##  [93] "Pakistan"                                          
##  [94] "Papua New Guinea"                                  
##  [95] "Paraguay"                                          
##  [96] "Peru"                                              
##  [97] "Philippines"                                       
##  [98] "Russian Federation"                                
##  [99] "Rwanda"                                            
## [100] "Samoa"                                             
## [101] "Sao Tome and Principe"                             
## [102] "Senegal"                                           
## [103] "Serbia"                                            
## [104] "Sierra Leone"                                      
## [105] "Solomon Islands"                                   
## [106] "Somalia"                                           
## [107] "South Africa"                                      
## [108] "South Asia"                                        
## [109] "Sri Lanka"                                         
## [110] "St. Lucia"                                         
## [111] "St. Vincent and the Grenadines"                    
## [112] "Sub-Saharan Africa (excluding high income)"        
## [113] "Sudan"                                             
## [114] "Syrian Arab Republic"                              
## [115] "Tajikistan"                                        
## [116] "Tanzania"                                          
## [117] "Thailand"                                          
## [118] "Timor-Leste"                                       
## [119] "Togo"                                              
## [120] "Tonga"                                             
## [121] "Tunisia"                                           
## [122] "Turkey"                                            
## [123] "Turkmenistan"                                      
## [124] "Uganda"                                            
## [125] "Ukraine"                                           
## [126] "Upper middle income"                               
## [127] "Uzbekistan"                                        
## [128] "Vanuatu"                                           
## [129] "Venezuela, RB"                                     
## [130] "Vietnam"                                           
## [131] "Yemen, Rep."                                       
## [132] "Zambia"                                            
## [133] "Zimbabwe"
debt_data[, unique(series_name)]
##   [1] "Disbursements on external debt, general government sector (PPG) (DIS, current US$)"                 
##   [2] "Disbursements on external debt, long-term (DIS, current US$)"                                       
##   [3] "Disbursements on external debt, long-term + IMF (DIS, current US$)"                                 
##   [4] "Disbursements on external debt, other public sector (PPG) (DIS, current US$)"                       
##   [5] "Disbursements on external debt, private guaranteed by public sector (PPG) (DIS, current US$)"       
##   [6] "Disbursements on external debt, private nonguaranteed (PNG) (DIS, current US$)"                     
##   [7] "Disbursements on external debt, public and publicly guaranteed (PPG) (DIS, current US$)"            
##   [8] "Disbursements on external debt, public sector (PPG) (DIS, current US$)"                             
##   [9] "GG, bilateral (DIS, current US$)"                                                                   
##  [10] "GG, bilateral concessional (DIS, current US$)"                                                      
##  [11] "GG, bonds (DIS, current US$)"                                                                       
##  [12] "GG, commercial banks (DIS, current US$)"                                                            
##  [13] "GG, multilateral (DIS, current US$)"                                                                
##  [14] "GG, multilateral concessional (DIS, current US$)"                                                   
##  [15] "GG, official creditors (DIS, current US$)"                                                          
##  [16] "GG, other private creditors (DIS, current US$)"                                                     
##  [17] "GG, private creditors (DIS, current US$)"                                                           
##  [18] "IMF purchases (DIS, current US$)"                                                                   
##  [19] "OPS, bilateral (DIS, current US$)"                                                                  
##  [20] "OPS, bilateral concessional (DIS, current US$)"                                                     
##  [21] "OPS, bonds (DIS, current US$)"                                                                      
##  [22] "OPS, commercial banks (DIS, current US$)"                                                           
##  [23] "OPS, multilateral (DIS, current US$)"                                                               
##  [24] "OPS, multilateral concessional (DIS, current US$)"                                                  
##  [25] "OPS, official creditors (DIS, current US$)"                                                         
##  [26] "OPS, other private creditors (DIS, current US$)"                                                    
##  [27] "OPS, private creditors (DIS, current US$)"                                                          
##  [28] "PNG, bonds (DIS, current US$)"                                                                      
##  [29] "PNG, commercial banks and other creditors (DIS, current US$)"                                       
##  [30] "PPG, bilateral (DIS, current US$)"                                                                  
##  [31] "PPG, bilateral concessional (DIS, current US$)"                                                     
##  [32] "PPG, bonds (DIS, current US$)"                                                                      
##  [33] "PPG, commercial banks (DIS, current US$)"                                                           
##  [34] "PPG, IBRD (DIS, current US$)"                                                                       
##  [35] "PPG, IDA (DIS, current US$)"                                                                        
##  [36] "PPG, multilateral (DIS, current US$)"                                                               
##  [37] "PPG, multilateral concessional (DIS, current US$)"                                                  
##  [38] "PPG, official creditors (DIS, current US$)"                                                         
##  [39] "PPG, other private creditors (DIS, current US$)"                                                    
##  [40] "PPG, private creditors (DIS, current US$)"                                                          
##  [41] "PRVG, bilateral (DIS, current US$)"                                                                 
##  [42] "PRVG, bilateral concessional (DIS, current US$)"                                                    
##  [43] "PRVG, bonds (DIS, current US$)"                                                                     
##  [44] "PRVG, commercial banks (DIS, current US$)"                                                          
##  [45] "PRVG, multilateral (DIS, current US$)"                                                              
##  [46] "PRVG, multilateral concessional (DIS, current US$)"                                                 
##  [47] "PRVG, official creditors (DIS, current US$)"                                                        
##  [48] "PRVG, other private creditors (DIS, current US$)"                                                   
##  [49] "PRVG, private creditors (DIS, current US$)"                                                         
##  [50] "PS, bilateral (DIS, current US$)"                                                                   
##  [51] "PS, bilateral concessional (DIS, current US$)"                                                      
##  [52] "PS, bonds (DIS, current US$)"                                                                       
##  [53] "PS, commercial banks (DIS, current US$)"                                                            
##  [54] "PS, multilateral (DIS, current US$)"                                                                
##  [55] "PS, multilateral concessional (DIS, current US$)"                                                   
##  [56] "PS, official creditors (DIS, current US$)"                                                          
##  [57] "PS, other private creditors (DIS, current US$)"                                                     
##  [58] "PS, private creditors (DIS, current US$)"                                                           
##  [59] "Portfolio investment, equity (DRS, current US$)"                                                    
##  [60] "Primary income on FDI (current US$)"                                                                
##  [61] "Average grace period on new external debt commitments (years)"                                      
##  [62] "Average grace period on new external debt commitments, official (years)"                            
##  [63] "Average grace period on new external debt commitments, private (years)"                             
##  [64] "Average grant element on new external debt commitments (%)"                                         
##  [65] "Average grant element on new external debt commitments, official (%)"                               
##  [66] "Average grant element on new external debt commitments, private (%)"                                
##  [67] "Average interest on new external debt commitments (%)"                                              
##  [68] "Average interest on new external debt commitments, official (%)"                                    
##  [69] "Average interest on new external debt commitments, private (%)"                                     
##  [70] "Average maturity on new external debt commitments (years)"                                          
##  [71] "Average maturity on new external debt commitments, official (years)"                                
##  [72] "Average maturity on new external debt commitments, private (years)"                                 
##  [73] "Commitments, bilateral creditors (COM, current US$)"                                                
##  [74] "Commitments, official creditors (COM, current US$)"                                                 
##  [75] "Commitments, private creditors (COM, current US$)"                                                  
##  [76] "Commitments, public and publicly guaranteed (COM, current US$)"                                     
##  [77] "Concessional debt (% of total external debt)"                                                       
##  [78] "Currency composition of PPG debt, all other currencies (%)"                                         
##  [79] "Currency composition of PPG debt, Deutsche mark (%)"                                                
##  [80] "Currency composition of PPG debt, French franc (%)"                                                 
##  [81] "Currency composition of PPG debt, Multiple currencies (%)"                                          
##  [82] "Currency composition of PPG debt, Pound sterling (%)"                                               
##  [83] "Currency composition of PPG debt, Swiss franc (%)"                                                  
##  [84] "Currency composition of PPG debt, U.S. dollars (%)"                                                 
##  [85] "Debt service on external debt, general government sector (PPG) (TDS, current US$)"                  
##  [86] "Debt service on external debt, long-term (TDS, current US$)"                                        
##  [87] "Debt service on external debt, other public sector (PPG) (TDS, current US$)"                        
##  [88] "Debt service on external debt, public and publicly guaranteed (PPG) (TDS, current US$)"             
##  [89] "Debt service on external debt, public sector (PPG) (TDS, current US$)"                              
##  [90] "Debt service on external debt, total (TDS, current US$)"                                            
##  [91] "External debt stocks (% of GNI)"                                                                    
##  [92] "External debt stocks, concessional (DOD, current US$)"                                              
##  [93] "External debt stocks, general government sector (PPG) (DOD, current US$)"                           
##  [94] "External debt stocks, long-term (DOD, current US$)"                                                 
##  [95] "External debt stocks, long-term public sector (DOD, current US$)"                                   
##  [96] "External debt stocks, other public sector (PPG) (DOD, current US$)"                                 
##  [97] "External debt stocks, public and publicly guaranteed (PPG) (DOD, current US$)"                      
##  [98] "External debt stocks, public sector (PPG) (DOD, current US$)"                                       
##  [99] "External debt stocks, total (DOD, current US$)"                                                     
## [100] "External debt stocks, variable rate (DOD, current US$)"                                             
## [101] "Foreign direct investment, net inflows in reporting economy (DRS, current US$)"                     
## [102] "GG, bilateral (AMT, current US$)"                                                                   
## [103] "GG, bilateral (DOD, current US$)"                                                                   
## [104] "GG, bilateral (INT, current US$)"                                                                   
## [105] "GG, bilateral (NFL, current US$)"                                                                   
## [106] "GG, bilateral (NTR, current US$)"                                                                   
## [107] "GG, bilateral (TDS, current US$)"                                                                   
## [108] "GG, bilateral concessional (DOD, current US$)"                                                      
## [109] "GG, bilateral concessional (NFL, current US$)"                                                      
## [110] "GG, bilateral concessional (NTR, current US$)"                                                      
## [111] "GG, official creditors (AMT, current US$)"                                                          
## [112] "GG, official creditors (DOD, current US$)"                                                          
## [113] "GG, official creditors (INT, current US$)"                                                          
## [114] "GG, official creditors (NFL, current US$)"                                                          
## [115] "GG, official creditors (NTR, current US$)"                                                          
## [116] "GG, official creditors (TDS, current US$)"                                                          
## [117] "GNI (current US$)"                                                                                  
## [118] "Grants, excluding technical cooperation (current US$)"                                              
## [119] "IDA grants (current US$)"                                                                           
## [120] "IFC, private nonguaranteed (NFL, current US$)"                                                      
## [121] "Interest payments on external debt (% of GNI)"                                                      
## [122] "Interest payments on external debt, general government sector (PPG) (INT, current US$)"             
## [123] "Interest payments on external debt, long-term (INT, current US$)"                                   
## [124] "Interest payments on external debt, other public sector (PPG) (INT, current US$)"                   
## [125] "Interest payments on external debt, public and publicly guaranteed (PPG) (INT, current US$)"        
## [126] "Interest payments on external debt, public sector (PPG) (INT, current US$)"                         
## [127] "Interest payments on external debt, total (INT, current US$)"                                       
## [128] "Multilateral debt (% of total external debt)"                                                       
## [129] "Multilateral debt service (TDS, current US$)"                                                       
## [130] "Net financial flows, bilateral (NFL, current US$)"                                                  
## [131] "Net financial flows, IBRD (NFL, current US$)"                                                       
## [132] "Net financial flows, multilateral (NFL, current US$)"                                               
## [133] "Net flows on external debt, general government sector (PPG) (NFL, current US$)"                     
## [134] "Net flows on external debt, long-term (NFL, current US$)"                                           
## [135] "Net flows on external debt, other public sector (PPG) (NFL, current US$)"                           
## [136] "Net flows on external debt, public and publicly guaranteed (PPG) (NFL, current US$)"                
## [137] "Net flows on external debt, public sector (PPG) (NFL, current US$)"                                 
## [138] "Net flows on external debt, total (NFL, current US$)"                                               
## [139] "Net transfers on external debt, general government sector (PPG) (NTR, current US$)"                 
## [140] "Net transfers on external debt, long-term (NTR, current US$)"                                       
## [141] "Net transfers on external debt, other public sector (PPG) (NTR, current US$)"                       
## [142] "Net transfers on external debt, public and publicly guaranteed (PPG) (NTR, current US$)"            
## [143] "Net transfers on external debt, public sector (PPG) (NTR, current US$)"                             
## [144] "Net transfers on external debt, total (NTR, current US$)"                                           
## [145] "OPS, bilateral (AMT, current US$)"                                                                  
## [146] "OPS, bilateral (DOD, current US$)"                                                                  
## [147] "OPS, bilateral (INT, current US$)"                                                                  
## [148] "OPS, bilateral (NFL, current US$)"                                                                  
## [149] "OPS, bilateral (NTR, current US$)"                                                                  
## [150] "OPS, bilateral (TDS, current US$)"                                                                  
## [151] "OPS, bilateral concessional (DOD, current US$)"                                                     
## [152] "OPS, bonds (DOD, current US$)"                                                                      
## [153] "OPS, bonds (NFL, current US$)"                                                                      
## [154] "OPS, bonds (NTR, current US$)"                                                                      
## [155] "OPS, commercial banks (AMT, current US$)"                                                           
## [156] "OPS, commercial banks (DOD, current US$)"                                                           
## [157] "OPS, commercial banks (INT, current US$)"                                                           
## [158] "OPS, commercial banks (NFL, current US$)"                                                           
## [159] "OPS, commercial banks (NTR, current US$)"                                                           
## [160] "OPS, commercial banks (TDS, current US$)"                                                           
## [161] "OPS, multilateral (AMT, current US$)"                                                               
## [162] "OPS, multilateral (DOD, current US$)"                                                               
## [163] "OPS, multilateral (INT, current US$)"                                                               
## [164] "OPS, multilateral (NFL, current US$)"                                                               
## [165] "OPS, multilateral (NTR, current US$)"                                                               
## [166] "OPS, multilateral (TDS, current US$)"                                                               
## [167] "OPS, official creditors (AMT, current US$)"                                                         
## [168] "OPS, official creditors (DOD, current US$)"                                                         
## [169] "OPS, official creditors (INT, current US$)"                                                         
## [170] "OPS, official creditors (NFL, current US$)"                                                         
## [171] "OPS, official creditors (NTR, current US$)"                                                         
## [172] "OPS, official creditors (TDS, current US$)"                                                         
## [173] "OPS, other private creditors (AMT, current US$)"                                                    
## [174] "OPS, other private creditors (DOD, current US$)"                                                    
## [175] "OPS, other private creditors (INT, current US$)"                                                    
## [176] "OPS, other private creditors (NFL, current US$)"                                                    
## [177] "OPS, other private creditors (NTR, current US$)"                                                    
## [178] "OPS, other private creditors (TDS, current US$)"                                                    
## [179] "OPS, private creditors (AMT, current US$)"                                                          
## [180] "OPS, private creditors (DOD, current US$)"                                                          
## [181] "OPS, private creditors (INT, current US$)"                                                          
## [182] "OPS, private creditors (NFL, current US$)"                                                          
## [183] "OPS, private creditors (NTR, current US$)"                                                          
## [184] "OPS, private creditors (TDS, current US$)"                                                          
## [185] "Personal transfers and compensation of employees, received (current US$)"                           
## [186] "PPG, bilateral (AMT, current US$)"                                                                  
## [187] "PPG, bilateral (DOD, current US$)"                                                                  
## [188] "PPG, bilateral (INT, current US$)"                                                                  
## [189] "PPG, bilateral (NTR, current US$)"                                                                  
## [190] "PPG, bilateral (TDS, current US$)"                                                                  
## [191] "PPG, bilateral concessional (DOD, current US$)"                                                     
## [192] "PPG, bilateral concessional (NFL, current US$)"                                                     
## [193] "PPG, bilateral concessional (NTR, current US$)"                                                     
## [194] "PPG, bonds (DOD, current US$)"                                                                      
## [195] "PPG, bonds (NFL, current US$)"                                                                      
## [196] "PPG, bonds (NTR, current US$)"                                                                      
## [197] "PPG, commercial banks (AMT, current US$)"                                                           
## [198] "PPG, commercial banks (DOD, current US$)"                                                           
## [199] "PPG, commercial banks (INT, current US$)"                                                           
## [200] "PPG, commercial banks (NFL, current US$)"                                                           
## [201] "PPG, commercial banks (NTR, current US$)"                                                           
## [202] "PPG, commercial banks (TDS, current US$)"                                                           
## [203] "PPG, IBRD (AMT, current US$)"                                                                       
## [204] "PPG, IBRD (DOD, current US$)"                                                                       
## [205] "PPG, IBRD (INT, current US$)"                                                                       
## [206] "PPG, IBRD (NTR, current US$)"                                                                       
## [207] "PPG, IBRD (TDS, current US$)"                                                                       
## [208] "PPG, multilateral (AMT, current US$)"                                                               
## [209] "PPG, multilateral (DOD, current US$)"                                                               
## [210] "PPG, multilateral (INT, current US$)"                                                               
## [211] "PPG, multilateral (NTR, current US$)"                                                               
## [212] "PPG, official creditors (AMT, current US$)"                                                         
## [213] "PPG, official creditors (DOD, current US$)"                                                         
## [214] "PPG, official creditors (INT, current US$)"                                                         
## [215] "PPG, official creditors (NFL, current US$)"                                                         
## [216] "PPG, official creditors (NTR, current US$)"                                                         
## [217] "PPG, official creditors (TDS, current US$)"                                                         
## [218] "PPG, other private creditors (AMT, current US$)"                                                    
## [219] "PPG, other private creditors (DOD, current US$)"                                                    
## [220] "PPG, other private creditors (INT, current US$)"                                                    
## [221] "PPG, other private creditors (NFL, current US$)"                                                    
## [222] "PPG, other private creditors (NTR, current US$)"                                                    
## [223] "PPG, other private creditors (TDS, current US$)"                                                    
## [224] "PPG, private creditors (AMT, current US$)"                                                          
## [225] "PPG, private creditors (DOD, current US$)"                                                          
## [226] "PPG, private creditors (INT, current US$)"                                                          
## [227] "PPG, private creditors (NFL, current US$)"                                                          
## [228] "PPG, private creditors (NTR, current US$)"                                                          
## [229] "PPG, private creditors (TDS, current US$)"                                                          
## [230] "Principal repayments on external debt, general government sector (PPG) (AMT, current US$)"          
## [231] "Principal repayments on external debt, long-term (AMT, current US$)"                                
## [232] "Principal repayments on external debt, long-term + IMF (AMT, current US$)"                          
## [233] "Principal repayments on external debt, other public sector (PPG) (AMT, current US$)"                
## [234] "Principal repayments on external debt, public and publicly guaranteed (PPG) (AMT, current US$)"     
## [235] "Principal repayments on external debt, public sector (PPG) (AMT, current US$)"                      
## [236] "PS, bilateral (AMT, current US$)"                                                                   
## [237] "PS, bilateral (DOD, current US$)"                                                                   
## [238] "PS, bilateral (INT, current US$)"                                                                   
## [239] "PS, bilateral (NFL, current US$)"                                                                   
## [240] "PS, bilateral (NTR, current US$)"                                                                   
## [241] "PS, bilateral (TDS, current US$)"                                                                   
## [242] "PS, bilateral concessional (DOD, current US$)"                                                      
## [243] "PS, bilateral concessional (NFL, current US$)"                                                      
## [244] "PS, bilateral concessional (NTR, current US$)"                                                      
## [245] "PS, bonds (DOD, current US$)"                                                                       
## [246] "PS, bonds (NFL, current US$)"                                                                       
## [247] "PS, bonds (NTR, current US$)"                                                                       
## [248] "PS, commercial banks (AMT, current US$)"                                                            
## [249] "PS, commercial banks (DOD, current US$)"                                                            
## [250] "PS, commercial banks (INT, current US$)"                                                            
## [251] "PS, commercial banks (NFL, current US$)"                                                            
## [252] "PS, commercial banks (NTR, current US$)"                                                            
## [253] "PS, commercial banks (TDS, current US$)"                                                            
## [254] "PS, multilateral (AMT, current US$)"                                                                
## [255] "PS, multilateral (DOD, current US$)"                                                                
## [256] "PS, multilateral (INT, current US$)"                                                                
## [257] "PS, multilateral (NFL, current US$)"                                                                
## [258] "PS, multilateral (NTR, current US$)"                                                                
## [259] "PS, multilateral (TDS, current US$)"                                                                
## [260] "PS, official creditors (AMT, current US$)"                                                          
## [261] "PS, official creditors (DOD, current US$)"                                                          
## [262] "PS, official creditors (INT, current US$)"                                                          
## [263] "PS, official creditors (NFL, current US$)"                                                          
## [264] "PS, official creditors (NTR, current US$)"                                                          
## [265] "PS, official creditors (TDS, current US$)"                                                          
## [266] "PS, other private creditors (AMT, current US$)"                                                     
## [267] "PS, other private creditors (DOD, current US$)"                                                     
## [268] "PS, other private creditors (INT, current US$)"                                                     
## [269] "PS, other private creditors (NFL, current US$)"                                                     
## [270] "PS, other private creditors (NTR, current US$)"                                                     
## [271] "PS, other private creditors (TDS, current US$)"                                                     
## [272] "PS, private creditors (AMT, current US$)"                                                           
## [273] "PS, private creditors (DOD, current US$)"                                                           
## [274] "PS, private creditors (INT, current US$)"                                                           
## [275] "PS, private creditors (NFL, current US$)"                                                           
## [276] "PS, private creditors (NTR, current US$)"                                                           
## [277] "PS, private creditors (TDS, current US$)"                                                           
## [278] "Technical cooperation grants (current US$)"                                                         
## [279] "Total reserves (% of total external debt)"                                                          
## [280] "Total reserves (includes gold, current US$)"                                                        
## [281] "Undisbursed external debt, official creditors (UND, current US$)"                                   
## [282] "Undisbursed external debt, private creditors (UND, current US$)"                                    
## [283] "Undisbursed external debt, total (UND, current US$)"                                                
## [284] "Commitments, multilateral creditors (COM, current US$)"                                             
## [285] "Currency composition of PPG debt, Japanese yen (%)"                                                 
## [286] "Debt service on external debt, private guaranteed by public sector (PPG) (TDS, current US$)"        
## [287] "Debt service on external debt, private nonguaranteed (PNG) (TDS, current US$)"                      
## [288] "External debt stocks, long-term private sector (DOD, current US$)"                                  
## [289] "External debt stocks, private guaranteed by public sector (PPG) (DOD, current US$)"                 
## [290] "External debt stocks, private nonguaranteed (PNG) (DOD, current US$)"                               
## [291] "External debt stocks, short-term (DOD, current US$)"                                                
## [292] "GG, bilateral concessional (INT, current US$)"                                                      
## [293] "GG, bilateral concessional (TDS, current US$)"                                                      
## [294] "GG, bonds (AMT, current US$)"                                                                       
## [295] "GG, bonds (DOD, current US$)"                                                                       
## [296] "GG, bonds (INT, current US$)"                                                                       
## [297] "GG, bonds (NFL, current US$)"                                                                       
## [298] "GG, bonds (NTR, current US$)"                                                                       
## [299] "GG, bonds (TDS, current US$)"                                                                       
## [300] "GG, commercial banks (AMT, current US$)"                                                            
## [301] "GG, commercial banks (DOD, current US$)"                                                            
## [302] "GG, commercial banks (INT, current US$)"                                                            
## [303] "GG, commercial banks (NFL, current US$)"                                                            
## [304] "GG, commercial banks (NTR, current US$)"                                                            
## [305] "GG, commercial banks (TDS, current US$)"                                                            
## [306] "GG, multilateral (AMT, current US$)"                                                                
## [307] "GG, multilateral (DOD, current US$)"                                                                
## [308] "GG, multilateral (INT, current US$)"                                                                
## [309] "GG, multilateral (NFL, current US$)"                                                                
## [310] "GG, multilateral (NTR, current US$)"                                                                
## [311] "GG, multilateral (TDS, current US$)"                                                                
## [312] "GG, multilateral concessional (AMT, current US$)"                                                   
## [313] "GG, multilateral concessional (DOD, current US$)"                                                   
## [314] "GG, multilateral concessional (INT, current US$)"                                                   
## [315] "GG, multilateral concessional (NFL, current US$)"                                                   
## [316] "GG, multilateral concessional (NTR, current US$)"                                                   
## [317] "GG, multilateral concessional (TDS, current US$)"                                                   
## [318] "GG, other private creditors (AMT, current US$)"                                                     
## [319] "GG, other private creditors (DOD, current US$)"                                                     
## [320] "GG, other private creditors (INT, current US$)"                                                     
## [321] "GG, other private creditors (NFL, current US$)"                                                     
## [322] "GG, other private creditors (NTR, current US$)"                                                     
## [323] "GG, other private creditors (TDS, current US$)"                                                     
## [324] "GG, private creditors (AMT, current US$)"                                                           
## [325] "GG, private creditors (DOD, current US$)"                                                           
## [326] "GG, private creditors (INT, current US$)"                                                           
## [327] "GG, private creditors (NFL, current US$)"                                                           
## [328] "GG, private creditors (NTR, current US$)"                                                           
## [329] "GG, private creditors (TDS, current US$)"                                                           
## [330] "Interest payments on external debt, private guaranteed by public sector (PPG) (INT, current US$)"   
## [331] "Interest payments on external debt, private nonguaranteed (PNG) (INT, current US$)"                 
## [332] "Net financial flows, RDB concessional (NFL, current US$)"                                           
## [333] "Net financial flows, RDB nonconcessional (NFL, current US$)"                                        
## [334] "Net flows on external debt, private guaranteed by public sector (PPG) (NFL, current US$)"           
## [335] "Net flows on external debt, private nonguaranteed (PNG) (NFL, current US$)"                         
## [336] "Net flows on external debt, short-term (NFL, current US$)"                                          
## [337] "Net transfers on external debt, private guaranteed by public sector (PPG) (NTR, current US$)"       
## [338] "Net transfers on external debt, private nonguaranteed (PNG) (NTR, current US$)"                     
## [339] "OPS, bilateral concessional (INT, current US$)"                                                     
## [340] "OPS, bilateral concessional (NFL, current US$)"                                                     
## [341] "OPS, bilateral concessional (NTR, current US$)"                                                     
## [342] "OPS, bilateral concessional (TDS, current US$)"                                                     
## [343] "OPS, bonds (AMT, current US$)"                                                                      
## [344] "OPS, bonds (INT, current US$)"                                                                      
## [345] "OPS, bonds (TDS, current US$)"                                                                      
## [346] "OPS, multilateral concessional (AMT, current US$)"                                                  
## [347] "OPS, multilateral concessional (DOD, current US$)"                                                  
## [348] "OPS, multilateral concessional (INT, current US$)"                                                  
## [349] "OPS, multilateral concessional (NFL, current US$)"                                                  
## [350] "OPS, multilateral concessional (NTR, current US$)"                                                  
## [351] "OPS, multilateral concessional (TDS, current US$)"                                                  
## [352] "PNG, commercial banks and other creditors (AMT, current US$)"                                       
## [353] "PNG, commercial banks and other creditors (DOD, current US$)"                                       
## [354] "PNG, commercial banks and other creditors (INT, current US$)"                                       
## [355] "PNG, commercial banks and other creditors (NFL, current US$)"                                       
## [356] "PNG, commercial banks and other creditors (NTR, current US$)"                                       
## [357] "PNG, commercial banks and other creditors (TDS, current US$)"                                       
## [358] "PPG, bilateral concessional (INT, current US$)"                                                     
## [359] "PPG, bilateral concessional (TDS, current US$)"                                                     
## [360] "PPG, bonds (AMT, current US$)"                                                                      
## [361] "PPG, bonds (INT, current US$)"                                                                      
## [362] "PPG, bonds (TDS, current US$)"                                                                      
## [363] "PPG, multilateral concessional (AMT, current US$)"                                                  
## [364] "PPG, multilateral concessional (DOD, current US$)"                                                  
## [365] "PPG, multilateral concessional (INT, current US$)"                                                  
## [366] "PPG, multilateral concessional (NFL, current US$)"                                                  
## [367] "PPG, multilateral concessional (NTR, current US$)"                                                  
## [368] "PPG, multilateral concessional (TDS, current US$)"                                                  
## [369] "Principal repayments on external debt, private guaranteed by public sector (PPG) (AMT, current US$)"
## [370] "Principal repayments on external debt, private nonguaranteed (PNG) (AMT, current US$)"              
## [371] "PRVG, bilateral (AMT, current US$)"                                                                 
## [372] "PRVG, bilateral (DOD, current US$)"                                                                 
## [373] "PRVG, bilateral (INT, current US$)"                                                                 
## [374] "PRVG, bilateral (NFL, current US$)"                                                                 
## [375] "PRVG, bilateral (NTR, current US$)"                                                                 
## [376] "PRVG, bilateral (TDS, current US$)"                                                                 
## [377] "PRVG, commercial banks (AMT, current US$)"                                                          
## [378] "PRVG, commercial banks (DOD, current US$)"                                                          
## [379] "PRVG, commercial banks (INT, current US$)"                                                          
## [380] "PRVG, commercial banks (NFL, current US$)"                                                          
## [381] "PRVG, commercial banks (NTR, current US$)"                                                          
## [382] "PRVG, commercial banks (TDS, current US$)"                                                          
## [383] "PRVG, multilateral (AMT, current US$)"                                                              
## [384] "PRVG, multilateral (DOD, current US$)"                                                              
## [385] "PRVG, multilateral (INT, current US$)"                                                              
## [386] "PRVG, multilateral (NFL, current US$)"                                                              
## [387] "PRVG, multilateral (NTR, current US$)"                                                              
## [388] "PRVG, multilateral (TDS, current US$)"                                                              
## [389] "PRVG, official creditors (AMT, current US$)"                                                        
## [390] "PRVG, official creditors (DOD, current US$)"                                                        
## [391] "PRVG, official creditors (INT, current US$)"                                                        
## [392] "PRVG, official creditors (NFL, current US$)"                                                        
## [393] "PRVG, official creditors (NTR, current US$)"                                                        
## [394] "PRVG, official creditors (TDS, current US$)"                                                        
## [395] "PRVG, other private creditors (AMT, current US$)"                                                   
## [396] "PRVG, other private creditors (DOD, current US$)"                                                   
## [397] "PRVG, other private creditors (INT, current US$)"                                                   
## [398] "PRVG, other private creditors (NFL, current US$)"                                                   
## [399] "PRVG, other private creditors (NTR, current US$)"                                                   
## [400] "PRVG, other private creditors (TDS, current US$)"                                                   
## [401] "PRVG, private creditors (AMT, current US$)"                                                         
## [402] "PRVG, private creditors (DOD, current US$)"                                                         
## [403] "PRVG, private creditors (INT, current US$)"                                                         
## [404] "PRVG, private creditors (NFL, current US$)"                                                         
## [405] "PRVG, private creditors (NTR, current US$)"                                                         
## [406] "PRVG, private creditors (TDS, current US$)"                                                         
## [407] "PS, bilateral concessional (INT, current US$)"                                                      
## [408] "PS, bilateral concessional (TDS, current US$)"                                                      
## [409] "PS, bonds (AMT, current US$)"                                                                       
## [410] "PS, bonds (INT, current US$)"                                                                       
## [411] "PS, bonds (TDS, current US$)"                                                                       
## [412] "PS, multilateral concessional (AMT, current US$)"                                                   
## [413] "PS, multilateral concessional (DOD, current US$)"                                                   
## [414] "PS, multilateral concessional (INT, current US$)"                                                   
## [415] "PS, multilateral concessional (NFL, current US$)"                                                   
## [416] "PS, multilateral concessional (NTR, current US$)"                                                   
## [417] "PS, multilateral concessional (TDS, current US$)"                                                   
## [418] "Short-term debt (% of total external debt)"                                                         
## [419] "Commitments, IDA (COM, current US$)"                                                                
## [420] "GG, bilateral concessional (AMT, current US$)"                                                      
## [421] "Interest arrears, long-term DOD (US$)"                                                              
## [422] "Interest arrears, official creditors (current US$)"                                                 
## [423] "Interest arrears, private creditors (current US$)"                                                  
## [424] "Net financial flows, IDA (NFL, current US$)"                                                        
## [425] "PPG, bilateral concessional (AMT, current US$)"                                                     
## [426] "PPG, IDA (DOD, current US$)"                                                                        
## [427] "PPG, IDA (INT, current US$)"                                                                        
## [428] "PPG, IDA (NTR, current US$)"                                                                        
## [429] "PPG, IDA (TDS, current US$)"                                                                        
## [430] "Principal arrears, long-term DOD (US$)"                                                             
## [431] "Principal arrears, official creditors (current US$)"                                                
## [432] "Principal arrears, private creditors (current US$)"                                                 
## [433] "PS, bilateral concessional (AMT, current US$)"                                                      
## [434] "Use of IMF credit (DOD, current US$)"                                                               
## [435] "Commitments, IBRD (COM, current US$)"                                                               
## [436] "IMF repurchases (AMT, current US$)"                                                                 
## [437] "IMF repurchases and charges (TDS, current US$)"                                                     
## [438] "Net financial flows, IMF nonconcessional (NFL, current US$)"                                        
## [439] "PRVG, bilateral concessional (DOD, current US$)"                                                    
## [440] "PRVG, multilateral concessional (AMT, current US$)"                                                 
## [441] "PRVG, multilateral concessional (DOD, current US$)"                                                 
## [442] "PRVG, multilateral concessional (INT, current US$)"                                                 
## [443] "PRVG, multilateral concessional (NFL, current US$)"                                                 
## [444] "PRVG, multilateral concessional (NTR, current US$)"                                                 
## [445] "PRVG, multilateral concessional (TDS, current US$)"                                                 
## [446] "OPS, bilateral concessional (AMT, current US$)"                                                     
## [447] "Current account balance (current US$)"                                                              
## [448] "Exports of goods, services and primary income (current US$)"                                        
## [449] "External debt stocks (% of exports of goods, services and primary income)"                          
## [450] "Imports of goods, services and primary income (current US$)"                                        
## [451] "Interest payments on external debt (% of exports of goods, services and primary income)"            
## [452] "Total debt service (% of exports of goods, services and primary income)"                            
## [453] "Total reserves in months of imports"                                                                
## [454] "PRVG, bilateral concessional (INT, current US$)"                                                    
## [455] "PRVG, bilateral concessional (NFL, current US$)"                                                    
## [456] "PRVG, bilateral concessional (NTR, current US$)"                                                    
## [457] "PRVG, bilateral concessional (TDS, current US$)"                                                    
## [458] "Debt forgiveness grants (current US$)"                                                              
## [459] "IMF charges (INT, current US$)"                                                                     
## [460] "Net financial flows, others (NFL, current US$)"                                                     
## [461] "Interest payments on external debt, short-term (INT, current US$)"                                  
## [462] "Net financial flows, IMF concessional (NFL, current US$)"                                           
## [463] "PRVG, bonds (AMT, current US$)"                                                                     
## [464] "PRVG, bonds (DOD, current US$)"                                                                     
## [465] "PRVG, bonds (INT, current US$)"                                                                     
## [466] "PRVG, bonds (NFL, current US$)"                                                                     
## [467] "PRVG, bonds (NTR, current US$)"                                                                     
## [468] "PRVG, bonds (TDS, current US$)"                                                                     
## [469] "PPG, IDA (AMT, current US$)"                                                                        
## [470] "PRVG, bilateral concessional (AMT, current US$)"                                                    
## [471] "Currency composition of PPG debt, SDR (%)"                                                          
## [472] "Residual, debt stock-flow reconciliation (current US$)"                                             
## [473] "Total change in external debt stocks (current US$)"                                                 
## [474] "Debt stock rescheduled (current US$)"                                                               
## [475] "Interest rescheduled (capitalized) (current US$)"                                                   
## [476] "Interest rescheduled, official (current US$)"                                                       
## [477] "Interest rescheduled, private (current US$)"                                                        
## [478] "Net change in interest arrears (current US$)"                                                       
## [479] "Principal rescheduled (current US$)"                                                                
## [480] "Principal rescheduled, official (current US$)"                                                      
## [481] "Principal rescheduled, private (current US$)"                                                       
## [482] "Total amount of debt rescheduled (current US$)"                                                     
## [483] "Debt forgiveness or reduction (current US$)"                                                        
## [484] "Debt stock reduction (current US$)"                                                                 
## [485] "Principal forgiven (current US$)"                                                                   
## [486] "Interest forgiven (current US$)"                                                                    
## [487] "Debt buyback (current US$)"                                                                         
## [488] "PNG, bonds (DOD, current US$)"                                                                      
## [489] "PNG, bonds (NFL, current US$)"                                                                      
## [490] "PNG, bonds (NTR, current US$)"                                                                      
## [491] "PNG, bonds (INT, current US$)"                                                                      
## [492] "PNG, bonds (TDS, current US$)"                                                                      
## [493] "PNG, bonds (AMT, current US$)"                                                                      
## [494] "Use of IMF credit, SDR allocations (DOD, current US$)"                                              
## [495] "Currency composition of PPG debt, Euro (%)"                                                         
## [496] "Present value of external debt (current US$)"
#"Uganda", "Tanzania"
ea_country <- c("Kenya", "Lower middle income" )


debt_data <- debt_data[country_name %in% ea_country & series_name %in% indicator_name]
debt_data_split <- split(debt_data, f = debt_data$series_name)

n <- length(debt_data_split)
my_plots <-htmltools::tagList()
for (i in 1:n) {
    df = debt_data_split[[i]]
    my_title = df[, unique(series_name)]
    mn = df[, min(year)]
    mx = df[, max(year)]
    breaks = seq(mn, mx,by = 3)
    p = ggplot(df, aes(year, value, group = country_name, color = country_name) ) +
        geom_line()+
        theme_hc()+
        labs(title = my_title, x = "year", y = "%")+
      scale_color_viridis_d(name="")+
      scale_x_continuous(breaks = breaks)
    
    my_plots[[i]] = ggplotly(p)
    
}

my_plots